package com.mediacloud.app.newsmodule.fragment.baoliao.activity;

import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.chinamcloud.collect.AcquisitionManager;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.BaoLiaoMeta;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.model.news.kt.TypeX;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.activity.StyleSetActivity;
import com.mediacloud.app.newsmodule.fragment.baoliao.frag.BaoLiaoDetailFragment;
import com.mediacloud.app.newsmodule.utils.DomainUtil;
import com.mediacloud.app.newsmodule.utils.support.SupportKit;

import com.mediacloud.app.user.model.UserInfo;
import com.utils.UriTool;

import io.reactivex.functions.Consumer;

/***
 * //烛料的网页js调用
 * appfactory://包名/type19?id=爆料id&share_url=爆料的分享url地址&status=爆料的状态（纯数字）
 */
public class BaoLiaoDetailActivity extends StyleSetActivity implements Consumer<Pair<Integer, BaoLiaoMeta>> {
    private long id;
    int status;

    BaoLiaoDetailFragment fragment;

    public ArticleItem getArticleItem() {
        return articleItem;
    }

    @Override
    public int getLayoutResID() {
        return R.layout.activity_bao_liao_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        articleItem = new ArticleItem();
        Uri uriData = getIntent().getData();
        if (uriData != null) {
            isFromH5OpenToReStart =UriTool.INSTANCE.isFromH5OpenToReStart(this,uriData);
            catalogItem = new CatalogItem();
            catalogItem.setCatalog_type("36");
            try {
                id = Integer.valueOf(uriData.getQueryParameter("id"));
            } catch (Exception e) {
                id = 0;
            }

            try {
                status = Integer.valueOf(uriData.getQueryParameter("status"));
            } catch (Exception e) {
                status = 1;
            }
            articleItem.setUrl(uriData.getQueryParameter("share_url"));
        } else {
            id = getIntent().getLongExtra("id", 0);
            status = 1;
            try {
                String s = getIntent().getStringExtra("status");
                status = Integer.parseInt(s);
            } catch (Exception e) {
                e.printStackTrace();
                status = 1;
            }

            catalogItem = getIntent().getParcelableExtra("catalog");
            if (catalogItem == null) {
                catalogItem = new CatalogItem();
                catalogItem.setCatalog_type("36");
            }
            articleItem.setId(id);
            articleItem.setUrl(getIntent().getStringExtra("url"));
        }
        //  setTitle(R.string.baoliao_detail);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        fragment = new BaoLiaoDetailFragment();
        fragment.consumer = this;
        Bundle bundle = new Bundle();
        bundle.putLong("id", id);
        bundle.putInt("status", status);
        fragment.setArguments(bundle);
        transaction.add(R.id.baoliao_container, fragment);
        transaction.commitAllowingStateLoss();
        init();

        onPageStart();
        showPageTransition();

        shareWrap.setArticleItem(articleItem);
    }


    public void setArticleItem(BaoLiaoMeta meta) {
        if (meta.getStatus() == 2) {
            mTitlebar_MoreContainer.setVisibility(View.VISIBLE);
            bottomBarLayout.setVisibility(View.VISIBLE);
        } else {
            mTitlebar_MoreContainer.setVisibility(View.GONE);
            bottomBarLayout.setVisibility(View.GONE);

        }
        articleItem.setSummary(meta.getSummary());
        articleItem.setId(meta.getId());
        articleItem.setTitle(meta.getTitle());
        articleItem.setSupportCount(meta.getFavorCount());
        if(TextUtils.isEmpty(meta.getIsComment())){
            articleItem.setIsComment("1");
        }else {
            articleItem.setIsComment(meta.getIsComment());
        }
        articleItem.setLogo(meta.getLogo());
        articleItem.setType(TypeX.NEWS.BROKING);
        articleItem.setCommentCount(meta.getCommentCount());
        articleItem.setCatalogId(meta.getCatalogId());
        articleItem.setUrl(meta.getShareUrl());
//        refreshBottomStyle(articleItem, 5);
        shareWrap.setArticleItem(articleItem);
        setLikeButtonNumb(5);
        UserInfo info = UserInfo.getUserInfo(this);
        AcquisitionManager.getInstance().readExit(info, "" + articleItem.getId(),
                DomainUtil.getDomain(articleItem.getUrl()), articleItem.getUrl(), articleItem.getTitle(), articleItem.getContent());
    }

    void init() {
        loadingView = findViewById(R.id.mLoadingView);
        mTitlebar_name = findViewById(R.id.mTitlebar_name);


    }


//    @Override
//    protected void setCommentData() {
//        if (appfacSB())
//            return;
//        super.setCommentData();
//    }


    @Override
    public void getLikeStatus() {
        try {
            if(likeBadgeView==null){
                Log.e("CCCXXX","1111111111111111111");
            }
            SupportKit.INSTANCE.initSupportViewState(this,SupportKit.TYPE_5,articleItem.getId(),likeBadgeView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    boolean appfacSB() {

        return status != 2;
    }

    @Override
    public void accept(Pair<Integer, BaoLiaoMeta> value) throws Exception {
        status = value.first;
        articleItem.setUrl(value.second.getShareHtml());
        articleItem.setLogo(value.second.getLogo());
    }


    @Override
    protected void onStart() {
        super.onStart();
        UserInfo info = UserInfo.getUserInfo(this);
        if (articleItem != null) {
            AcquisitionManager.getInstance().readEnter(info, "" + articleItem.getId(),
                    DomainUtil.getDomain(articleItem.getUrl()), articleItem.getUrl(), articleItem.getTitle(), articleItem.getContent());
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (articleItem != null) {
            UserInfo info = UserInfo.getUserInfo(this);
            AcquisitionManager.getInstance().readExit(info, "" + articleItem.getId(),
                    DomainUtil.getDomain(articleItem.getUrl()), articleItem.getUrl(), articleItem.getTitle(), articleItem.getContent());
        }

    }

}